﻿using FineUI;
using SysManage.BLL;
using System;
using System.Data;
using System.Linq;
using System.Text;
using ZHDJ.Core.Util;
using ZHDJ.Data.Models.Common;

namespace SysManage.Module.NotificationAnnouncement
{
    public partial class PartyNotificationAnnouncementEdit : PageBase
    {
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "";
            }
        }
        public string Category
        {
            get { return this.Request["Category"] ?? ""; }
        }

        public string Module
        {
            get { return this.Request["Module"] ?? "Announcement"; }
        }
        #endregion

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            string id = GetQueryIntValue("id");
            if (id != "")
            {
                PartyArticle article = DB.PartyArticles.Find(id);
                if (article == null)
                {
                    // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }

                txtTitle.Text = article.Title;
                heContent.Text = article.Content;

                string sql = string.Format(@"SELECT file.Title FROM tblpartyarticle as a
                                                INNER JOIN tblpartyarticlesfile as afile
                                                ON a.ID=afile.ArticleID
                                                LEFT JOIN tblpartyfile as file
                                                ON afile.FileID=file.ID
                                                WHERE a.ID='{0}';", article.ID);
                DataTable dt = ZHDJ.Data.MySqlHelper.ExecuteDataTable(sql);
                StringBuilder sb = new StringBuilder();
                foreach (DataRow dr in dt.Rows)
                {
                    sb.Append(dr["Title"]);
                    sb.Append(",");
                }
                txtFilePath.Value = sb.ToString();
            }
        }
        #endregion

        #region Events

        private void SaveData()
        {
            string id = GetQueryIntValue("id");
            string imageUrl = string.Empty;

            if (id != "")
            {
                //更新
                PartyArticle article = DB.PartyArticles.Find(id);
                if (article == null)
                {
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }

                article.Title = txtTitle.Text.Trim();
                article.Content = heContent.Text;
                article.ModifyTime = DateTime.Now;
                article.ModifyBy = CurrentUser.LoginID;

                //先删除之前的文件数据
                DB.PartyArticlesFiles.Where(u => u.ArticleID == article.ID).ToList().ForEach(u => DB.PartyArticlesFiles.Remove(u));
                AddFile(article);

                DB.SaveChanges();
            }
            else
            {
                PartyArticle article = new PartyArticle();
                article.ID = StringHelper.NewID;
                article.Title = txtTitle.Text.Trim();
                article.Content = heContent.Text;
                article.Module = this.Module;
                article.Category = this.Category;
                article.Status = 4;
                article.CreateTime = DateTime.Now;
                article.ModifyTime = DateTime.Now;
                article.CreateBy = CurrentUser.LoginID;
                article.ModifyBy = CurrentUser.LoginID;
                DB.PartyArticles.Add(article);

                AddFile(article);

                DB.SaveChanges();
            }
        }

        private void AddFile(PartyArticle article)
        {
            string[] pathArray = txtFileID.Value.TrimEnd(',').Split(',');

            foreach (var item in pathArray)
            {
                PartyArticlesFile file = new PartyArticlesFile();
                file.ID = StringHelper.NewID;
                file.ArticleID = article.ID;
                file.FileID = item;
                file.FileType = 0;
                file.Status = 0;
                file.OrderNo = 0;
                file.CreateTime = DateTime.Now;
                file.CreateBy = CurrentUser.LoginID;
                file.ModifyBy = CurrentUser.LoginID;
                file.ModifyTime = DateTime.Now;

                DB.PartyArticlesFiles.Add(file);
            }
        }

        /// <summary>
        /// 保存并关闭按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveData();

            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion
    }
}